
namespace FastFood.DataAccess
{
	using System;
	using System.Collections.Generic;
	using System.Data;
	using System.Data.Common;
	using Microsoft.Practices.EnterpriseLibrary.Data;
	using EDOSwit.Entity;
	using FastFood.Business.Entities;

	/// <summary>
	/// Represents the DataAccess of SemiProductos_InsumosData.
	/// </summary>
	public class SemiProductos_InsumosData : Singleton<SemiProductos_InsumosData>
	{
		#region Attributes

		private readonly Database BaseDatos = DatabaseFactory.CreateDatabase();

		#endregion

		#region Methods

		/// <summary>
		/// Selects all records from the SemiProductos_Insumos table by a foreign key.
		/// </summary>
		public IList<SemiProductos_Insumos> Listar()
		{
			var entidades = new List<SemiProductos_Insumos>();
			var comando = BaseDatos.GetStoredProcCommand("SelectAllSemiProductos_Insumos");

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					entidades.Add(new SemiProductos_Insumos
					{
						semiProductoID = lector.IsDBNull(lector.GetOrdinal("semiProductoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("semiProductoID")),
						insumoID = lector.IsDBNull(lector.GetOrdinal("insumoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("insumoID")),
						cantidad = MathExpression.Round(Convert.ToSingle(lector.GetValue(lector.GetOrdinal("cantidad"))))
					});
				}
			}
			comando.Dispose();
			return entidades;
		}

		/// <summary>
		/// Selects all records from the SemiProductos_Insumos table by a foreign key.
		/// </summary>
		public SemiProductos_Insumos Obtener(short semiProductoID, short insumoID)
		{
			var semiProductos_Insumos = default(SemiProductos_Insumos);
			var comando = BaseDatos.GetStoredProcCommand("SelectSemiProductos_Insumos");

			BaseDatos.AddInParameter(comando, "semiProductoID", DbType.Int16, semiProductoID);
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, insumoID);

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					semiProductos_Insumos = new SemiProductos_Insumos
					{
						semiProductoID = lector.IsDBNull(lector.GetOrdinal("semiProductoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("semiProductoID")),
						insumoID = lector.IsDBNull(lector.GetOrdinal("insumoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("insumoID")),
                        cantidad = MathExpression.Round(Convert.ToSingle(lector.GetValue(lector.GetOrdinal("cantidad"))))
					};
				}
			}
			comando.Dispose();
			return semiProductos_Insumos;
		}

		/// <summary>
		/// Saves a record to the SemiProductos_Insumos table.
		/// </summary>
		public int Agregar(short semiTerminadoID, SemiProductos_Insumos semiProductos_Insumos, DbTransaction transaccion)
		{
			var comando = BaseDatos.GetStoredProcCommand("InsertSemiProductos_Insumos");
            BaseDatos.AddInParameter(comando, "semiProductoID", DbType.Int16, semiTerminadoID);
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, semiProductos_Insumos.insumoID);
			BaseDatos.AddInParameter(comando, "cantidad", DbType.Single, MathExpression.Round(semiProductos_Insumos.cantidad));

			var resultado = BaseDatos.ExecuteNonQuery(comando, transaccion);
			if (resultado == 0) throw new Exception("Error al Agregar detalle de SemiTerminado.");

			comando.Dispose();
			return 1;
		}

		/// <summary>
		/// Updates a record in the SemiProductos_Insumos table.
		/// </summary>
		public bool Modificar(SemiProductos_Insumos semiProductos_Insumos)
		{
			var comando = BaseDatos.GetStoredProcCommand("UpdateSemiProductos_Insumos");
			BaseDatos.AddInParameter(comando, "semiProductoID", DbType.Int16, semiProductos_Insumos.semiProductoID);
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, semiProductos_Insumos.insumoID);
			BaseDatos.AddInParameter(comando, "cantidad", DbType.Single, MathExpression.Round(semiProductos_Insumos.cantidad));

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Modificar SemiProductos_Insumos.");

			comando.Dispose();
			return true;
		}

		/// <summary>
		/// Deletes a record from the SemiProductos_Insumos table by its primary key.
		/// </summary>
		public bool Eliminar(short semiProductoID, short insumoID)
		{
			var comando = BaseDatos.GetStoredProcCommand("DeleteSemiProductos_Insumos");
			BaseDatos.AddInParameter(comando, "semiProductoID", DbType.Int16, semiProductoID);
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, insumoID);

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Eliminar detalle de SemiTerminado.");
			comando.Dispose();
			return true;
		}

		/// <summary>
		/// Deletes a record from the SemiProductos_Insumos table by a foreign key.
		/// </summary>
		public void EliminarPorInsumoID(short insumoID)
		{
			var comando = BaseDatos.GetStoredProcCommand("DeleteSemiProductos_InsumosByInsumoID");
			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, insumoID);

			var resultado = BaseDatos.ExecuteNonQuery(comando);
			if (resultado == 0) throw new Exception("Error al Eliminar SemiProductos_Insumos.");
			comando.Dispose();
		}

		/// <summary>
		/// Deletes a record from the SemiProductos_Insumos table by a foreign key.
		/// </summary>
		public void EliminarPorSemiProductoID(short semiProductoID, DbTransaction transaccion)
		{
			var comando = BaseDatos.GetStoredProcCommand("DeleteSemiProductos_InsumosBySemiProductoID");
			BaseDatos.AddInParameter(comando, "semiProductoID", DbType.Int16, semiProductoID);

            var resultado = BaseDatos.ExecuteNonQuery(comando, transaccion);
            if (resultado == 0) throw new Exception("Error al Eliminar detalle de SemiTerminado.");
			comando.Dispose();
		}

		/// <summary>
		/// Selects all records from the SemiProductos_Insumos table by a foreign key.
		/// </summary>
		public IList<SemiProductos_Insumos> ListarPorInsumoID(short insumoID)
		{
			var entidades = new List<SemiProductos_Insumos>();
			var comando = BaseDatos.GetStoredProcCommand("SelectSemiProductos_InsumosByInsumoID");

			BaseDatos.AddInParameter(comando, "insumoID", DbType.Int16, insumoID);

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					entidades.Add(new SemiProductos_Insumos
					{
						semiProductoID = lector.IsDBNull(lector.GetOrdinal("semiProductoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("semiProductoID")),
						insumoID = lector.IsDBNull(lector.GetOrdinal("insumoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("insumoID")),
                        cantidad = MathExpression.Round(Convert.ToSingle(lector.GetValue(lector.GetOrdinal("cantidad"))))
					});
				}
			}
			comando.Dispose();
			return entidades;
		}

		/// <summary>
		/// Selects all records from the SemiProductos_Insumos table by a foreign key.
		/// </summary>
		public IList<SemiProductos_Insumos> ListarPorSemiProductoID(short semiProductoID)
		{
			var entidades = new List<SemiProductos_Insumos>();
			var comando = BaseDatos.GetStoredProcCommand("SelectSemiProductos_InsumosBySemiProductoID");

			BaseDatos.AddInParameter(comando, "semiProductoID", DbType.Int16, semiProductoID);

			using (var lector = BaseDatos.ExecuteReader(comando))
			{
				while (lector.Read())
				{
					SemiProductos_Insumos item = new SemiProductos_Insumos();
					item.semiProductoID = lector.IsDBNull(lector.GetOrdinal("semiProductoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("semiProductoID"));
					item.insumoID = lector.IsDBNull(lector.GetOrdinal("insumoID"))? default(short) : lector.GetInt16(lector.GetOrdinal("insumoID"));
                    item.cantidad = MathExpression.Round(Convert.ToSingle(lector.GetValue(lector.GetOrdinal("cantidad"))));
                    item.nombreInsumo = lector.IsDBNull(lector.GetOrdinal("insumo")) ? default(string) : lector.GetString(lector.GetOrdinal("insumo"));
                    item.stockInsumo = MathExpression.Round(lector.GetDouble(lector.GetOrdinal("stock")));
                    entidades.Add(item);
				}
			}
			comando.Dispose();
			return entidades;
		}

		#endregion
	}
}
